package com.ruoyi.energy.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.energy.domain.TcStatistics;
import com.ruoyi.energy.domain.vo.TcStatisticsWXVO;
import com.ruoyi.energy.mapper.TcStatisticsMapper;
import com.ruoyi.energy.service.ITcStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 停车场统计Service业务层处理
 *
 * @author cjie
 * @date 2022-04-25
 */
@Service
public class TcStatisticsServiceImpl implements ITcStatisticsService {
    @Autowired
    private TcStatisticsMapper tcStatisticsMapper;

    /**
     * 查询停车场统计
     *
     * @param id 停车场统计ID
     * @return 停车场统计
     */
    @Override
    public TcStatistics selectTcStatisticsById(String id) {
        return tcStatisticsMapper.selectTcStatisticsById(id);
    }

    /**
     * 查询停车场统计列表
     *
     * @param tcStatistics 停车场统计
     * @return 停车场统计
     */
    @Override
    public List<TcStatistics> selectTcStatisticsList(TcStatistics tcStatistics) {
        return tcStatisticsMapper.selectTcStatisticsList(tcStatistics);
    }

    /**
     * 新增停车场统计
     *
     * @param tcStatistics 停车场统计
     * @return 结果
     */
    @Override
    public int insertTcStatistics(TcStatistics tcStatistics) {
        tcStatistics.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        tcStatistics.setCreateTime(DateUtils.getNowDate());
        tcStatistics.setCreateBy(SecurityUtils.getUsername());
        return tcStatisticsMapper.insertTcStatistics(tcStatistics);
    }

    /**
     * 修改停车场统计
     *
     * @param tcStatistics 停车场统计
     * @return 结果
     */
    @Override
    public int updateTcStatistics(TcStatistics tcStatistics) {
        tcStatistics.setUpdateTime(DateUtils.getNowDate());
        tcStatistics.setUpdateBy(SecurityUtils.getUsername());
        return tcStatisticsMapper.updateTcStatistics(tcStatistics);
    }

    /**
     * 批量删除停车场统计
     *
     * @param ids 需要删除的停车场统计ID
     * @return 结果
     */
    @Override
    public int deleteTcStatisticsByIds(String[] ids) {
        return tcStatisticsMapper.deleteTcStatisticsByIds(ids);
    }

    /**
     * 删除停车场统计信息
     *
     * @param id 停车场统计ID
     * @return 结果
     */
    @Override
    public int deleteTcStatisticsById(String id) {
        return tcStatisticsMapper.deleteTcStatisticsById(id);
    }

    @Override
    public int genStaticByDate(String staticDate) {
        List<TcStatistics> genByDates = tcStatisticsMapper.genStaticByChargeType(staticDate);
        if(genByDates == null || genByDates.size() == 0){
            return 0;
        }
        TcStatistics result = new TcStatistics();
        //按parkId 分组，子组汇总
        for(TcStatistics tcStatistics:genByDates){
            result.setReceivableAccount(result.getReceivableAccount().add(tcStatistics.getReceivableAccount()));;
            result.setReceivedAccount(result.getReceivedAccount().add(tcStatistics.getReceivedAccount()));
            result.setDaikouAccount(result.getDaikouAccount().add(tcStatistics.getDaikouAccount()));
            if(tcStatistics.getChargeType().equals("微信支付")){
                result.setWxAccount(result.getWxAccount().add(tcStatistics.getReceivedAccount()));
            }else if(tcStatistics.getChargeType().equals("现金支付")){
                result.setCashAccount(result.getCashAccount().add(tcStatistics.getReceivedAccount()));
            }
            result.setParkId(tcStatistics.getParkId());
            result.setParkingName(tcStatistics.getParkingName());
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(staticDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        result.setStaticDate(date);
        //存在更新，不存在插入
        TcStatistics search = new TcStatistics();
        search.setParkId(result.getParkId());
        search.setStaticDate(date);
        List<TcStatistics> updateList = tcStatisticsMapper.selectTcStatisticsList(search);
        if(updateList != null && updateList.size() >0){
            result.setId(updateList.get(0).getId());
            return this.updateTcStatistics(result);
        }else {
            return this.insertTcStatistics(result);
        }

    }

    @Override
    public TcStatisticsWXVO selectTcStatisticsWXVO(TcStatisticsWXVO wxvo){
        TcStatisticsWXVO result = new TcStatisticsWXVO();
        // 月度统计，月度统计 0
        if(wxvo.getStatisticType().equals("0")){
            List<TcStatistics> list = tcStatisticsMapper.selectTcStatisticsMonthWXVO(wxvo);
            result.getTcStatisticsList().addAll(list);
            TcStatistics tool = new TcStatistics();
            for(TcStatistics tmp:list){
                tool.setDaikouAccount(tool.getDaikouAccount().add(tmp.getDaikouAccount()));
                tool.setCashAccount(tool.getCashAccount().add(tmp.getCashAccount()));
                tool.setWxAccount(tool.getWxAccount().add(tmp.getWxAccount()));
                tool.setReceivedAccount(tool.getReceivedAccount().add(tmp.getReceivedAccount()));
                tool.setReceivableAccount(tool.getReceivableAccount().add(tmp.getReceivableAccount()));
            }
            result.setToolPanel(tool);
        }else{
            List<TcStatistics> list = tcStatisticsMapper.selectTcStatisticsYearWXVO(wxvo);
            //年度统计 1
            Map<String,List<TcStatistics>> resultMap = get(list);
            TcStatistics yearTool = new TcStatistics();
            //按月分类 累加
            Iterator<String> iterator = resultMap.keySet().iterator();
            while(iterator.hasNext()){
                String key = iterator.next();
                List<TcStatistics> listTc = resultMap.get(key);
                if(!listTc.isEmpty()){
                    TcStatistics month = new TcStatistics();
                    month.setStaticDate(listTc.get(0).getStaticDate());
                    for(TcStatistics tmp:listTc){
                        month.setDaikouAccount(month.getDaikouAccount().add(tmp.getDaikouAccount()));
                        month.setCashAccount(month.getCashAccount().add(tmp.getCashAccount()));
                        month.setWxAccount(month.getWxAccount().add(tmp.getWxAccount()));
                        month.setReceivedAccount(month.getReceivedAccount().add(tmp.getReceivedAccount()));
                        month.setReceivableAccount(month.getReceivableAccount().add(tmp.getReceivableAccount()));
                    }
                    result.getTcStatisticsList().add(month);
                    yearTool.setDaikouAccount(yearTool.getDaikouAccount().add(month.getDaikouAccount()));
                    yearTool.setCashAccount(yearTool.getCashAccount().add(month.getCashAccount()));
                    yearTool.setWxAccount(yearTool.getWxAccount().add(month.getWxAccount()));
                    yearTool.setReceivedAccount(yearTool.getReceivedAccount().add(month.getReceivedAccount()));
                    yearTool.setReceivableAccount(yearTool.getReceivableAccount().add(month.getReceivableAccount()));
                }

            }
            result.setToolPanel(yearTool);

        }

        return result;
    }
    //按月份分类,list有序降序
    private Map<String,List<TcStatistics>> get(List<TcStatistics> list){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Map<String,List<TcStatistics>> map = new LinkedHashMap<>();
        for(TcStatistics tcStatistic:list){
            String keyString = simpleDateFormat.format(tcStatistic.getStaticDate());
            if(!map.containsKey(keyString)){
                map.put(keyString,new ArrayList<>());
            }
            map.get(keyString).add(tcStatistic);
        }
        return map;
    }
}
